package com.example.wm.SanWar.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.SanWar.VO.AssingmentVO;
import com.example.wm.SanWar.entity.AssignmentEntity;
import com.example.wm.SanWar.entity.CommunicationEntity;
import com.example.wm.SanWar.entity.Goalgroup;
import com.example.wm.SanWar.mapper.AssignmentMapper;
import com.example.wm.SanWar.mapper.GoalgroupMapper;
import com.example.wm.SanWar.service.AssignmentService;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.utils.TsyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/AssignmentController")
public class AssignmentController {
    @Resource
    private AssignmentService assignmentService;
    @Resource
    private AssignmentMapper assignmentMapper;
    @Resource
    private GoalgroupMapper goalgroupMapper;
    @Resource
    private TsyUtils tsyUtils;
    @Transactional
    @PostMapping("/Insert")
    public Result Insert(@RequestBody AssignmentEntity assignmentEntity) {
        assignmentEntity.setCreatetime(tsyUtils.getnowtime());

        // 将字符串解析为 LocalDateTime 对象
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime now= LocalDateTime.parse(tsyUtils.getnowtime(),formatter);
//        LocalDateTime dateTime1 = LocalDateTime.parse(assignmentEntity.getStarttime(), formatter);
//        LocalDateTime dateTime2 = LocalDateTime.parse(assignmentEntity.getEndtime(), formatter);
//        if (dateTime1.isAfter(now)) {
//            assignmentEntity.setStatus("未开始");
//        } else if (dateTime1.isBefore(now) ) {
//            assignmentEntity.setStatus("进行中");
//        } else {
//            assignmentEntity.setStatus("进行中");
//        }
        assignmentService.save(assignmentEntity);
//        assignmentEntity.getId();
//        for(Goalgroup g:goalgroupList){
//            g.setGroupid(assignmentEntity.getId());
//            goalgroupMapper.insert(g);
//        }

        return ResultData.success( assignmentEntity.getId());
    }
    @Transactional
    @PostMapping("/Insertg")
    public Result Insertg(@RequestBody  Goalgroup goalgroup) {


          int b=  goalgroupMapper.insert(goalgroup);

        return ResultData.success( b);
    }
    @Transactional
    @PostMapping("/update")
    public Result update(@RequestBody AssignmentEntity assignmentEntity) {

        boolean b= assignmentService.updateById(assignmentEntity);
        LambdaQueryWrapper<Goalgroup> gl=new LambdaQueryWrapper<>();
        gl.eq(Goalgroup::getAid,assignmentEntity.getId());
     List<Goalgroup> goalgroupList=  goalgroupMapper.selectList(gl);
     Long n=   goalgroupMapper.selectCount(gl);

     if(n>0){
         for(Goalgroup gp:goalgroupList){
             goalgroupMapper.deleteById(gp.getId());



         }


     }


        return ResultData.success(assignmentEntity.getId());
    }

    @Transactional
    @PostMapping("/updatejustass")
    public Result updatejustass(@RequestBody AssignmentEntity assignmentEntity) {

        boolean b= assignmentService.updateById(assignmentEntity);

        return ResultData.success( b);
    }


    @Transactional
    @PostMapping("/updatejustAssign")
    public Result updatejustAssign(@RequestBody AssignmentEntity assignmentEntity) {

        boolean b= assignmentService.updateById(assignmentEntity);
        return ResultData.success( b);


    }
    @Transactional
    @PostMapping("/delete")
    public Result delete(@RequestBody AssignmentEntity assignmentEntity) {

        boolean b= assignmentService.removeById(assignmentEntity);
        LambdaQueryWrapper<Goalgroup> gl=new LambdaQueryWrapper<>();
        gl.eq(Goalgroup::getAid,assignmentEntity.getId());
        List<Goalgroup> goalgroupList=  goalgroupMapper.selectList(gl);
        Long n=   goalgroupMapper.selectCount(gl);
        if(n>0){
            for(Goalgroup gp:goalgroupList){
                goalgroupMapper.deleteById(gp.getId());
            }
        }
        return ResultData.success( b);
    }
    @Transactional
    @PostMapping("/findg")
    public Result findg(@RequestBody JSONObject jsonObject) {
        Page<AssignmentEntity> pageInfo = new Page<>(jsonObject.getLong("page"),jsonObject.getLong("pageSize"));
        LambdaQueryWrapper<AssignmentEntity> lq=new LambdaQueryWrapper<>();
        lq.eq(AssignmentEntity::getNameid,jsonObject.getString("nameid"));
         List<AssingmentVO> voList=new ArrayList<>();
        Page<AssignmentEntity>  lista=assignmentService.page(pageInfo,lq);
           if(lista.getSize()>0){

//        List<AssignmentEntity> lista=assignmentService.list(lq);
        for (AssignmentEntity al:lista.getRecords()){
            AssingmentVO as=new AssingmentVO();
            as.setAssignmentEntity( al);
            LambdaQueryWrapper<Goalgroup> gl=new LambdaQueryWrapper<>();
           gl.eq(Goalgroup::getAid,al.getId());
            as.setGoalgroupList( goalgroupMapper.selectList(gl));
            voList.add(as);
        }
           }

//


        return ResultData.success( voList);
    }

}
